<html>
	<head>
		<title>Test resources</title>
		<script type="text/javascript">
		function getReq() {
		    var req = false;
		    if(window.XMLHttpRequest) {
		        try {
		            req = new XMLHttpRequest();
		        } catch(e) {
		            req = false;
		        }
		    } else if(window.ActiveXObject) {
		        try {
		            req = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch(e) {
		            req = false;
		        }
		    }
		    if (! req) {
		        alert("Your browser does not support XMLHttpRequest.");
		    }
		    return req;
		}
		
		function postMethod() {
			var itemName = document.add_item.name.value;
			var itemDesc = document.add_item.description.value;
		    var req = getReq();
		    var success = true;
		    var errorMsg = "";
		    try {
		        req.open("POST", "/items", false);
		        req.send("name=" + itemName + "&description=" + itemDesc);
		    } catch (e) {
		    	errorMsg = "Error: " + e;
		    }
		
		    if (req.status != 201 && req.status != 304) {
		        alert("Add failed. " + errorMsg + ". Response status code: " + req.status + ". Message: " + req.responseText);
		    } else {
				alert("Add successful. Response status code: " + req.status + ". Message: " + req.responseText);
		    }
		}
		
		function putMethod() {
			var itemName = document.update_item.name.value;
			var itemDesc = document.update_item.description.value;
		    var req = getReq();
		    var success = true;
		    var errorMsg = "";
		    try {
		        req.open("PUT", "/items/" + itemName, false);
		        req.send("description=" + itemDesc);
		    } catch (e) {
		    	errorMsg = "Error: " + e;
		    }
		
		    if (req.status != 200 && req.status != 304) {
		        alert("Update failed. " + errorMsg + ". Response status code: " + req.status + ". Message: " + req.responseText);
		    } else {
				alert("Update successful. Response status code: " + req.status);
		    }
		}
		
		function deleteMethod() {
			var itemName = document.delete_item.name.value;
		    var req = getReq();
		    var success = true;
		    var errorMsg = "";
		    try {
		        req.open("DELETE", "/items/" + itemName, false);
		        req.send(null);
		    } catch (e) {
		    	errorMsg = "Error: " + e;
		    }
		
		    if (req.status != 204 && req.status != 304) {
		        alert("Delete failed. " + errorMsg + ". Response status code: " + req.status + ". Message: " + req.responseText);
		    } else {
				alert("Delete successful. Response status code: " + req.status);
		    }
		}
		</script>
		<style type="text/css">
			body {
				font-family:Arial,Helvetica,Sans serif;
				font-size:100%
			}
			h1 {
				font-size:2.5em
			}
			h2 {
				font-size:1.875em
			}
			p {
				font-size:0.875em
			}		
		</style>
	</head>
	<body>

		<h1>Test resources</h1>
		
		<p>[<a href="test.html">Reset</a> | <a href="/items" target="_blank">List items</a>]</p>
		
		<h2><strong>Add item:</strong></h2>
		
		<form name="add_item" onsubmit="return postMethod()">
			Name: <input id="add_item_name" type="text" name="name" value=""/>&nbsp;
			Desc: <input id="add_item_desc" type="text" name="description" value=""/>&nbsp;
			<input id="add_item_submit" type="submit" name="submit" value="submit"/>
		</form>
		
		<h2><strong>Update item:</strong></h2>
		
		<form name="update_item" onsubmit="return putMethod()">
			Name: <input id="update_item_name" type="text" name="name" value=""/>&nbsp;
			Desc: <input id="update_item_desc" type="text" name="description" value=""/>&nbsp;
			<input id="update_item_submit" type="submit" name="submit" value="submit"/>
		</form>
		
		<h2><strong>Delete item:</strong></h2>
		
		<form name="delete_item" onsubmit="return deleteMethod()">
			Name: <input id="delete_item_name" type="text" name="name" value=""/>&nbsp;
			<input id="delete_item_submit" type="submit" name="submit" value="submit"/>
		</form>
	
	</body>
</html>